﻿//#include rfm

(function() {

function _load(url, oncomplete) {
    var img = document.createElement('img');
    if (oncomplete) {
        img.onload = oncomplete;
        img.onerror = oncomplete;
    }
    img.src = url;
}

/**
 * Image utilities
 */
rfm.Image = {
    /**
     * Collects all distinct images in a DOM tree
     * @param {Element} root The root element
     * @param {Array} collection (Optional) The array into which to collect the images
     * @returns {Array<String>} A list of all image paths in the DOM tree
     */
    collect: function(root, collection) {
        collection = collection || [];
        var cssBg = window.getComputedStyle(root, '').getPropertyValue('background-image'),
            imgSrc = root.getAttribute('src'),
            child = root.firstChild,
            i;
        if (cssBg && cssBg != 'none') {
            cssBg = (cssBg.match(/^url\(\s*\'?\"?([^)'"]+)\"?\'?\s*\)$/) || {})[1];
            if (cssBg && collection.indexOf(cssBg) < 0) {
                collection[collection.length] = cssBg;
            }
        }
        if (imgSrc && collection.indexOf(imgSrc) < 0) {
            collection[collection.length] = imgSrc;
        }
        while (child) {
            if (child.nodeType == 1) {
                rfm.Image.collect(child, collection);
            }
            child = child.nextSibling;
        }
        return collection;
    },
    /**
     * Loads images so that they're ready to be displayed
     * @param {String} src The URL of the image to preload
     * @param {Function} oncomplete A callback to execute when the image has been loaded
     * -or-
     * @param {Array<String>} src A list of image URLs to preload
     * @param {Function} oncomplete A callback to execute when all of the images have been loaded
     * -or-
     * @param {Element} src The root element of a node tree that should be scanned for <IMG> elements and CSS background images to preload
     * @param {Function} oncomplete A callback to execute when all of the images have been loaded
     */
    preload: function(src, oncomplete) {
        if (src instanceof Element) {
            rfm.Image.preload(rfm.Image.collect(src), oncomplete);
        }
        else if (typeof src == 'string') {
            rfm.Image.preload([src], oncomplete);
        }
        else {
            var numLoaded = 0,
                totalNum = src.length,
                isComplete = false,
                imageURL,
                i;
            function onload_img() {
                numLoaded++;
                if (numLoaded >= totalNum && !isComplete) {
                    isComplete = true;
                    if (oncomplete) {
                        oncomplete();
                    }
                }
            }
            if (totalNum > 0) {
                for (i = totalNum - 1; i > -1; i--) {
                    _load(src[i], onload_img);
                }
            }
            else {
                onload_img();
            }
        }
    }
};

})();